package org.fastiot.data.sampling

class Strategy {

    static class Storage {
        Boolean latest
        Boolean timing
        Boolean hbase

        static constraints = {
            latest(nullable: false)
            timing(nullable: false)
            hbase(nullable: false)
        }
    }

    static class PushCSV {
        Boolean enable
        String attributes
        Boolean hdfs
        Boolean pgsql

        static constraints = {
            enable(nullable: false)
            attributes(nullable: true, matches: /\w+[,\w+]*/)
            hdfs(nullable: false)
            pgsql(nullable: false)
        }

        static mapping = {
            attributes type: "text"
        }
    }

    Integer packetCode
    String hexPacketCode

    Boolean detectionThreshold
    Storage storage
    PushCSV pushCSV

    static embedded = ['storage', 'pushCSV']

    static constraints = {
        hexPacketCode(blank: false, matches: /[0-9a-fA-F]+/)
        packetCode(nullable: false)
        detectionThreshold(nullable: false)
        storage(nullable: false)
        pushCSV(nullable: false)
    }

    def beforeValidate() {
        if (packetCode && !hexPacketCode)
            hexPacketCode = Integer.toHexString(packetCode)
        else if (hexPacketCode && !packetCode)
            packetCode = Integer.parseInt(hexPacketCode, 16)
    }

    def afterLoad() {
        hexPacketCode = Integer.toHexString(packetCode)
    }
}
